/*
 * Huawei Software Technologies Co., Ltd. Copyright 1998-2009, All rights reserved.
 * 文件名  :CSPResponseServiceLocator.java
 * 创建人  :yuebinyao
 * 创建时间:2013-3-18
 */
package com.cimp.service.c2.server;


import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.Remote;
import java.util.HashSet;
import java.util.Iterator;

import javax.xml.namespace.QName;
import javax.xml.rpc.ServiceException;

import org.apache.axis.AxisFault;
import org.apache.axis.EngineConfiguration;
import org.apache.axis.client.Service;
import org.apache.axis.client.Stub;

/**
 * 
 * [简要描述]:<br/>
 * [详细描述]:<br/>
 *
 * @version [revision],Aug 29, 2012
 * @since IPTV CMS V100R003C01B630
 */
public class CSPResponseServiceLocator extends Service implements CSPResponseService
{
    /**
     * 序列化
     */
    private static final long serialVersionUID = 3834992447858524512L;

    private String ctms_address = "";

    private String ctmsWSDDServiceName = "ctms";

    private HashSet ports = null;

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     *
     */
    public CSPResponseServiceLocator()
    {
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     *
     * @param config EngineConfiguration
     */
    public CSPResponseServiceLocator(EngineConfiguration config)
    {
        super(config);
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     *
     * @param wsdlLoc String
     * @param sName QName
     * @throws ServiceException ServiceException
     */
    public CSPResponseServiceLocator(String wsdlLoc, QName sName) throws ServiceException
    {
        super(wsdlLoc, sName);
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @return String
     * @exception
     */
    public String getctmsAddress()
    {
        return this.ctms_address;
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @return String
     * @exception
     */
    public String getctmsWSDDServiceName()
    {
        return this.ctmsWSDDServiceName;
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @param name String
     * @exception
     */
    public void setctmsWSDDServiceName(String name)
    {
        this.ctmsWSDDServiceName = name;
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @return CSPResponse
     * @throws ServiceException ServiceException
     * @exception
     */
    public CSPResponse getctms() throws ServiceException
    {
        URL endpoint;
        try
        {
            endpoint = new URL(this.ctms_address);
        }
        catch (MalformedURLException e)
        {
            throw new ServiceException(e);
        }
        return getctms(endpoint);
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @param portAddress URL
     * @return CSPResponse
     * @exception 
     */
    public CSPResponse getctms(URL portAddress)
    {
        CtmsSoapBindingStub stub = null;
        int timeout = 0;
        
        try
        {
            stub = new CtmsSoapBindingStub(portAddress, this);
            stub.setPortName(getctmsWSDDServiceName());

            timeout = 3 * 1000;
            stub.setTimeout(timeout);

            return stub;
        }
        catch (AxisFault e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @param address String
     * @exception
     */
    public void setctmsEndpointAddress(String address)
    {
        this.ctms_address = address;
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @param serviceEndpointInterface Class
     * @return Remote
     * @throws ServiceException ServiceException
     * @exception 
     * @see Service#getPort(Class)
     */
    public Remote getPort(Class serviceEndpointInterface) throws ServiceException
    {
        CtmsSoapBindingStub stub = null;
        
        try
        {
            if (CSPResponse.class.isAssignableFrom(serviceEndpointInterface))
            {
                stub = new CtmsSoapBindingStub(new URL(this.ctms_address), this);
                stub.setPortName(getctmsWSDDServiceName());
                return stub;
            }
        }
        catch (Throwable t)
        {
            throw new ServiceException(t);
        }
        throw new ServiceException("There is no stub implementation for the interface:  "
                + (serviceEndpointInterface == null ? "null" : serviceEndpointInterface.getName()));
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @param portName QName
     * @param serviceEndpointInterface Class
     * @return Remote
     * @throws ServiceException ServiceException
     * @exception 
     * @see Service#getPort(QName, Class)
     */
    public Remote getPort(QName portName, Class serviceEndpointInterface) throws ServiceException
    {
        if (portName == null)
        {
            return getPort(serviceEndpointInterface);
        }
        String inputPortName = portName.getLocalPart();
        if ("ctms".equals(inputPortName))
        {
            return getctms();
        }

        Remote stub = getPort(serviceEndpointInterface);
        ((Stub) stub).setPortName(portName);
        
        return stub;
    }

    public QName getServiceName()
    {
        return new QName("iptv", "CSPResponseService");
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @return Iterator
     * @exception 
     * @see Service#getPorts()
     */
    public Iterator getPorts()
    {
        if (this.ports == null)
        {
            this.ports = new HashSet();
            this.ports.add(new QName("iptv", "ctms"));
        }
        return this.ports.iterator();
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @param portName String
     * @param address String
     * @throws ServiceException ServiceException
     * @exception
     */
    public void setEndpointAddress(String portName, String address) throws ServiceException
    {
        if ("ctms".equals(portName))
        {
            setctmsEndpointAddress(address);
        }
        else
        {
            throw new ServiceException(" Cannot set Endpoint Address for Unknown Port" + portName);
        }
    }

    /**
     * 
     * [简要描述]:<br/>
     * [详细描述]:<br/>
     * 
     * @param portName QName
     * @param address String
     * @throws ServiceException ServiceException
     * @exception
     */
    public void setEndpointAddress(QName portName, String address) throws ServiceException
    {
        setEndpointAddress(portName.getLocalPart(), address);
    }
}
